package service.Impl;

import com.alibaba.fastjson.JSONObject;
import entity.KaoshitaskSetup;
import entity.KaoshitaskSetupA;
import mapper.KaoshitaskSetupDao;
import org.springframework.stereotype.Service;
import service.KaoshitaskSetupService;
import utils.PageObject;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.List;

/**
 * (KaoshitaskSetup)表服务实现类
 *
 * @author makejava
 * @since 2022-01-19 10:04:45
 */
@Service("kaoshitaskSetupService")
public class KaoshitaskSetupServiceImpl implements KaoshitaskSetupService {
    @Resource
    private KaoshitaskSetupDao kaoshitaskSetupDao;

    /**
     * 通过ID查询单条数据
     *
     * @param pkId 主键
     * @return 实例对象
     */
    @Override
    public KaoshitaskSetup queryById(Integer pkId) {
        return this.kaoshitaskSetupDao.queryById(pkId);
    }

    /**
     * 查询多条数据
     *
     * @return 对象列表
     */
    @Override
    public PageObject queryAllByLimit(JSONObject object) {
        PageObject pageObject = new PageObject();
        Integer page = object.getInteger("page");
        Integer limit = object.getInteger("limit");
        Integer offset = (page - 1) * limit;
        List<KaoshitaskSetup> kaoshitaskSetups1 = this.kaoshitaskSetupDao.queryAllByLimitSetup(offset, limit);

        List<KaoshitaskSetupA> kaoshitaskSetups = this.kaoshitaskSetupDao.queryAllByLimit(offset, limit);


        Integer selectcount = kaoshitaskSetupDao.selectcount();
        Integer selectcount2 = kaoshitaskSetupDao.selectcount2();
        pageObject.setPageCount(page);//第几页
        pageObject.setPageSize(limit);//每页几条
        pageObject.setRowCount(selectcount);//总条数
        pageObject.setRowCount2(selectcount2);//总条数
        pageObject.setPageCount(selectcount / limit);//总页数
        pageObject.setRecords(Collections.singletonList(kaoshitaskSetups1));//当前需展示数据
        pageObject.setRecords2(Collections.singletonList(kaoshitaskSetups));//当前需展示数据
        return pageObject;


    }

    /**
     * 新增数据
     *
     * @param kaoshitaskSetup 实例对象
     * @return 实例对象
     */
    @Override
    public KaoshitaskSetup insert(KaoshitaskSetup kaoshitaskSetup) {
        this.kaoshitaskSetupDao.insert(kaoshitaskSetup);
        return kaoshitaskSetup;
    }

    /**
     * 修改数据
     *
     * @param kaoshitaskSetup 实例对象
     * @return 实例对象
     */
    @Override
    public KaoshitaskSetup update(KaoshitaskSetup kaoshitaskSetup) {
        this.kaoshitaskSetupDao.update(kaoshitaskSetup);
        return this.queryById(kaoshitaskSetup.getPkId());
    }

    /**
     * 通过主键删除数据
     *
     * @param pkId 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(Integer pkId) {
        return this.kaoshitaskSetupDao.deleteById(pkId) > 0;
    }


    @Override
    public boolean xaunding(JSONObject object) {

        Integer valueId = object.getInteger("valueId");
        Integer pkId = object.getInteger("pkId");

        this.kaoshitaskSetupDao.insertxuanding(valueId, pkId);

        return true;

    }
}
